'    Copyright 2008 Jan Molnar <jan.molnar@abds.de>
'
'    This file is part of JCQ.

'    JCQ is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 2 of the License, or
'    (at your option) any later version.

'    JCQ is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with JCQ. If not, see <http://www.gnu.org/licenses/>.

Imports System
Imports System.Collections.Generic
Imports System.Xml

Imports Microsoft.VisualStudio.TestTools.UnitTesting

Imports JCsTools.Xml.Formatter

'''<summary>
'''This is a test class for XmlFormatterTest and is intended
'''to contain all XmlFormatterTest Unit Tests
'''</summary>
<TestClass()> _
Public Class XmlFormatterTest


    Private testContextInstance As TestContext

    '''<summary>
    '''Gets or sets the test context which provides
    '''information about and functionality for the current test run.
    '''</summary>
    Public Property TestContext() As TestContext
        Get
            Return testContextInstance
        End Get
        Set(ByVal value As TestContext)
            testContextInstance = Value
        End Set
    End Property

#Region "Additional test attributes"
    '
    'You can use the following additional attributes as you write your tests:
    '
    'Use ClassInitialize to run code before running the first test in the class
    '<ClassInitialize()>  _
    'Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)
    'End Sub
    '
    'Use ClassCleanup to run code after all tests in a class have run
    '<ClassCleanup()>  _
    'Public Shared Sub MyClassCleanup()
    'End Sub
    '
    'Use TestInitialize to run code before running each test
    '<TestInitialize()>  _
    'Public Sub MyTestInitialize()
    'End Sub
    '
    'Use TestCleanup to run code after each test has run
    '<TestCleanup()>  _
    'Public Sub MyTestCleanup()
    'End Sub
    '
#End Region

    '''<summary>
    '''A test for Serialize
    '''</summary>
    <TestMethod()> _
    Public Sub SerializeStructuredObjectTest()
        Dim target As XmlSerializer = New XmlSerializer

        Dim graph = New Person With {.FirstName = Environment.UserName, .Age = Environment.TickCount, _
        .Address = New Address With {.StreetName = Environment.SystemDirectory, .City = Environment.MachineName}}

        Dim rand As New Random()

        For i = 1 To 100
            graph.Data.Add(Convert.ToByte(rand.Next(Byte.MinValue, Byte.MaxValue)))
        Next

        graph.Girlfriends.Add("Angie") : graph.Girlfriends.Add("Sahra")

        target.InitializeForUnitTest()
        target.RegisterReferenceFormatter(GetType(List(Of Byte)), New ListOfByteFormatter(target))

        Using sw = New System.IO.StringWriter
            Dim writer As XmlWriter = New System.Xml.XmlTextWriter(sw)
            target.Serialize(graph, writer)

            Assert.IsFalse(String.IsNullOrEmpty(sw.ToString))
            Assert.Inconclusive(sw.ToString)
        End Using
    End Sub

    '''<summary>
    '''A test for Serialize
    '''</summary>
    <TestMethod()> _
    Public Sub DeserializeStructuredObjectTest2()
        Dim target As XmlSerializer = New XmlSerializer
        Dim xml As String = My.Resources.DummyDataDocument

        Dim graph As Person

        target.RegisterReferenceFormatter(GetType(List(Of Byte)), New ListOfByteFormatter(target))

        Using sr = New System.IO.StringReader(xml)
            Using reader = New System.Xml.XmlTextReader(sr)
                graph = DirectCast(target.Deserialize(reader), Person)

                Assert.IsNotNull(graph)
                Assert.AreEqual("Jan-Cornelius Molnar", graph.FirstName)
                Assert.AreEqual(12362486, graph.Age)

                Assert.IsNotNull(graph.Address)

                Assert.AreEqual("C:\Windows\system32", graph.Address.StreetName)
                Assert.AreEqual("JAN-NB", graph.Address.City)

                Assert.IsNotNull(graph.Girlfriends)

                Assert.IsTrue(graph.Girlfriends.Count = 2)

                Assert.AreEqual("Angie", graph.Girlfriends(0))
                Assert.AreEqual("Sahra", graph.Girlfriends(1))

                Assert.IsTrue(graph.Data.Count = 100)
            End Using
        End Using
    End Sub

    '''<summary>
    '''A test for XmlFormatter Constructor
    '''</summary>
    <TestMethod()> _
    Public Sub XmlFormatterConstructorTest()
        Dim target As XmlSerializer = New XmlSerializer
    End Sub
End Class
